<?php

class LoginController extends Controller
{
	public function actionIndex()
	{
		echo "Please login";
	}
	
	public function actionPerform()
	{
	
		$username = Yii::app()->request->getParam('username');
		$password = Yii::app()->request->getParam('password');
		
		if($username == null || $password == null)
		{
			$response = new ServerResponse(-100, "A username and password must be supplied.", null);
			$this->renderJSON($response);
		}
		
		$found = UserAccounts::model()->with('userRole')->findAllByAttributes(
						array(),
						$condition  = 'username = :username AND password = :password',
						$params     = array(
											':username' => $username, 
											':password' => $password
										)
				);
		
		if($found)
		{
			$model = $found[0];
			$user = new User();
			$user->username = $model->username;
			$user->role_name = $model->userRole->role_name;
			
			$response = new ServerResponse(100, "OK", $user);
		}
		else
		{
			$response = new ServerResponse(-100, "User Not Found", null);
		}

		$this->renderJSON($response);
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}